package nft

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"ppa/init/mysql"
	"ppa/model"
)

// GetNftInfo
// @Summary 获取NFT详情
// @Tags NFT中心
// @Produce  json
// @Description 根据NftId获取NFT详情
// @Param nft_id query int true "nft_id"
// @Success 200   {object} model.Nft
// @Router /nft/info [get]
func GetNftInfo(c *gin.Context) {
	var nft model.Nft
	if err := c.ShouldBind(&nft);err != nil{
		c.JSON(http.StatusBadRequest,gin.H{"message":err.Error()})
		return
	}
	resData,err:= nft.GetNftInfo()
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		return
	}
	c.JSON(http.StatusOK,gin.H{"data":resData})
	return
}


// SetNftInfo
// @Summary 添加新的NFT作品
// @Tags NFT中心
// @Produce  json
// @Description 添加新的NFT作品 例子:{"nft_name":"懵逼小艺术家","nft_images":"https://a.disquscdn.com/1646863724/images/noavatar92.png","user_id":1,"collection_id":1,"owner_user_id":1,"royalty":5,"nft_state":1,"nft_price":0.5,"categories_nft":[{"categories_id":3},{"categories_id":2}]}
// @Param nft_name query string true "nft_name"
// @Param nft_images query string true "nft_images"
// @Param user_id query int true "user_id"
// @Param royalty query int true "royalty"
// @Param owner_user_id query int true "owner_user_id"
// @Param nft_price query int true "nft_price"
// @Param nft_state query int true "nft_state"
// @Param collection_id query int true "collection_id"
// @Param categories_nft query model.CategoriesNft true "categories_nft"
// @Router /nft/info [post]
func SetNftInfo(c *gin.Context) {
	var nft model.Nft
	if err := c.ShouldBind(&nft);err != nil{
		c.JSON(http.StatusBadRequest,gin.H{"message":err.Error()})
		return
	}
	tx := mysql.SqlDB.Begin()
	resNft,err:= nft.SetNftInfo(tx)
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		tx.Rollback()
		return
	}
	//创建交易记录
	var nftTransient []model.NftTransient
	nftTransient = append(nftTransient,model.NftTransient{NftId:resNft.ID, HashTransaction: "",FromUserId: resNft.UserId,ToUserId: resNft.UserId,TransientPrice: 0,TransactionState: 0})
	if nft.NftState == 1 {
		nftTransient = append(nftTransient,model.NftTransient{NftId:resNft.ID, TransientPrice:resNft.NftPrice,HashTransaction: "",FromUserId: resNft.UserId,ToUserId: resNft.UserId,TransactionState: 1})
	}
	_,err = model.SetNftTransient(nftTransient,tx)
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		tx.Rollback()
		return
	}

	err = tx.Commit().Error
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		tx.Rollback()
		return
	}

	c.JSON(http.StatusOK,gin.H{"message":"插入成功"})
	return
}


// SetNftInfoByUserId
// @Summary NFT上下架操作
// @Tags NFT中心
// @Produce  json
// @Description NFT上下架操作
// @Param nft_id query int true "nft_id"
// @Param owner_user_id query int true "owner_user_id"
// @Param nft_state query int true "nft_state"
// @Param nft_price query int false "nft_price"
// @Router /nft/info/state [put]
func SetNftInfoByUserId(c *gin.Context) {
	var nft model.Nft
	if err := c.ShouldBind(&nft);err != nil{
		c.JSON(http.StatusBadRequest,gin.H{"message":err.Error()})
		return
	}
	_,err := nft.SetNftInfoByUserId()
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		return
	}
	var nftState uint8 = 3
	if nft.NftState == 1 {
		nftState = 1
	}

	var nftTransient []model.NftTransient
	nftTransient = append(nftTransient,model.NftTransient{NftId:nft.ID, HashTransaction: "",FromUserId: nft.OwnerUserId,ToUserId: nft.OwnerUserId,TransientPrice: nft.NftPrice,TransactionState: nftState})
	_,err = model.SetNftTransient(nftTransient,nil)
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		return
	}

	c.JSON(http.StatusOK,gin.H{"message":"更新成功"})
	return
}


// SetBuyNft
// @Summary NFT购买后回调 （以太确认成交后提交此接口）
// @Tags NFT中心
// @Produce  json
// @Description NFT购买后回调
// @Param nft_id query int true "nft_id"
// @Param owner_user_id query int true "owner_user_id"
// @Router /nft/info/buy [post]
func SetBuyNft(c *gin.Context) {
	var nft model.Nft
	if err := c.ShouldBind(&nft);err != nil{
		c.JSON(http.StatusBadRequest,gin.H{"message":err.Error()})
		return
	}
	tx := mysql.SqlDB.Begin()
	nftInfo,err := nft.SetBuyNftInfo(tx)
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		tx.Rollback()
		return
	}
	var nftTransient []model.NftTransient
	nftTransient = append(nftTransient,model.NftTransient{NftId:nft.ID, HashTransaction: "",FromUserId: nftInfo.OwnerUserId,ToUserId: nft.OwnerUserId,TransientPrice: nftInfo.NftPrice,TransactionState: 2})
	_,err = model.SetNftTransient(nftTransient,tx)
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		tx.Rollback()
		return
	}

	err = tx.Commit().Error
	if err != nil {
		c.JSON(http.StatusLengthRequired,gin.H{"message":err.Error()})
		tx.Rollback()
		return
	}
	c.JSON(http.StatusOK,gin.H{"message":"更新成功"})
	return
}


